package com.syni.mdd.yhd.common.all.service.impl.advert;

import java.util.List;
import java.util.Map;

import com.syni.mdd.yhd.common.all.service.ImgPrefixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.advert.BmsAdvertDao;
import com.syni.mdd.yhd.common.all.entity.advert.BmsAdvert;
import com.syni.mdd.yhd.common.all.service.advert.BmsAdvertService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
/**
 * @className
 * @description TOO
 * @Author lcc
 * @DATE 2020/1/14 10:20
 * @VERSION 1.0
 **/
@Service
public class BmsAdvertServiceImpl extends BaseService<BmsAdvert, Integer> implements BmsAdvertService{

	@Autowired
	BmsAdvertDao bmsAdvertDao;

	@Autowired
	ImgPrefixService imgPrefixService;
	
	@Autowired
	RedisTemplate redisTemplate;
	 
	@Override
	public BmsAdvertDao getBaseDao() {
		return bmsAdvertDao;
	}

	public boolean checkTime(BmsAdvert bmsAdvert){
        Integer nowTime = DateTimeUtils.getNowTimeStamp();
        return nowTime >= DateTimeUtils.converDateToInterger(bmsAdvert.getStartTime()) &&
				nowTime <= DateTimeUtils.converDateToInterger(bmsAdvert.getEndTime());
    }
	
	@Override
	public Map<String, Object> getAdverts() {
		if(redisTemplate.hasKey(Constants.REDIS_KEY_FULL_ADVERT_OPEN)){
			String flag= redisTemplate.opsForValue().get(Constants.REDIS_KEY_FULL_ADVERT_OPEN).toString();
			if(flag.equals(Constants.COMMON_FLAG_TRUE)) {
				 List<BmsAdvert> bmsAdverts = bmsAdvertDao.findAll();
			        for(int i=0;i<bmsAdverts.size();i++){
						if( !checkTime(bmsAdverts.get(i)) ){
							bmsAdverts.remove(i);
						}
					}
					imgPrefixService.pushSpliceField(bmsAdverts,"imgUrl","btnUrl","qrcodeUrl");

			        return ResultMapHelper.success(bmsAdverts,ResultMsg.MSG_GET_SUCCESS);
			}
		}
		
		return ResultMapHelper.result(ResultCode.CODE_ERROR,"no adverts");
        
	}

}
